package com.iter.devbox.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 点对点消息消费者
 * 
 * @author Jimly
 */
public class QueueConsumer {

	public void receive () {
		try {
			//点对点消息的接收
			ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
			Connection connection = connectionFactory.createConnection();
			connection.start();
			Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);//acknowledge承认
			Destination destination = session.createQueue("messageQueue");
			
			//点对点消息接收
			MessageConsumer messageConsumer = session.createConsumer(destination);
			while (true) {
				ObjectMessage _objectMessage = (ObjectMessage)messageConsumer.receive(15000);
				if (null != _objectMessage) {
					String message = (String)_objectMessage.getObject();
					System.out.println(message);
				}
			}
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}
